//pages/shops/shops.js
//获取应用实例
const app = getApp()

Page({
  data: {
    indicatorDots: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 800,
    imgUrls: [],
    Hei: "", //轮播图容器高度

    goodslistjf: [],
    goodslistjf1:[],
    goodslistjf2:[],
    goodslistjf3: [],
    curnum: 0,
    curpage: 1,
    numpage: 12,
    morewz:'点击加载更多>>',
    ism:0,

    typelist: [], // 分类列表
    currType: 1,
    curnum: 1,
    leftH: "",
  },

  imgH: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth;         //获取当前屏幕的宽度
    var imgh = e.detail.height;　　　　　　　　　　　　　　　　//图片高度
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw - 5 + "px"　　　　　//等比设置swiper的高度。    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      Hei: swiperH,　　　　　　　//设置高度
    });
    app.swiperH = swiperH;
  },

  // leftH: function (e) {
  //   var winWid = wx.getSystemInfoSync().windowWidth;         //获取当前屏幕的宽度
  //   var swiperH = getApp.swiperH;　　
  //   var leftH = winWid - swiperH - 70;
  //   this.setData({
  //     leftH: leftH,
  //   })
  // },

  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function (options) {
    var that = this;

    that.setData({
      sessionkey: wx.getStorageSync('sessionkey'),
    });

    //获取banner
    wx.request({
      url: app.d.hostUrl + '/api/system/getGuanggaoList',
      method: 'post',
      data: {
        name:'积分商品'
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
       //console.log(res);
       var imgUrls = res.data.data;
       that.setData({
          imgUrls: imgUrls,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

    //获取用户积分
    // wx.request({
    //   url: app.d.hostUrl + '/user/get_myintegral_info',
    //   method: 'post',
    //   data: {
    //     'sessionkey': wx.getStorageSync('sessionkey'),
    //   },
    //   header: {
    //     'Content-Type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //    // console.log(res);
    //     var curnum = res.data.errmsg.fans_integral;
    //     that.setData({
    //       curnum: curnum,
    //     });
    //   },
    //   fail: function (e) {
    //     wx.showToast({
    //       title: '网络异常！',
    //       duration: 2000
    //     });
    //   },
    // });

    //获取左侧分类列表
    wx.request({
      url: app.d.hostUrl + '/api/goods/getCategoryList',
      method: 'post',
      data: {
        type:1,
        sessionkey: wx.getStorageSync('sessionkey')
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.data);
        var typelist = res.data.data;
        that.setData({
          typelist: typelist,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

    //获取积分商品列表
    wx.request({
      url: app.d.hostUrl + '/api/goods/getGoodsList',
      method: 'post',
      data: {
        cate_id: 1,
        page: 1,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res);
        var goodslistjf = res.data.data;
        that.setData({
          goodslistjf: goodslistjf,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

  },


  tapType: function (e) {
    var that = this;
    var nowType = getApp().currType;
    let currType = e.currentTarget.dataset.id;
    app.currType = currType;
    app.curpage = 1;
    // console.log(nowType);
    // console.log(currType);
    if (nowType != currType) {
      that.setData({
        goodslistjf3: [],
        goodslistjf1: [],
      });
    }

    that.setData({
      currType: currType,
      goodslistjf1: [],
      ism: 0,
      morewz: '点击加载更多>>',
    });

    //获取积分商品
    wx.request({
      url: app.d.hostUrl + '/api/goods/getGoodsList',
      method: 'post',
      data: {
        cate_id: currType,
        page:1,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res);
        var goodslistjf = res.data.data;
        that.setData({
          goodslistjf: goodslistjf,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

  },

  // 点击加载更多
  getMore: function (e) {
    var that = this; 
    var currType = getApp().currType;
    var curp= getApp().curpage + 1;

    //获取积分商品
    wx.request({
      url: app.d.hostUrl + '/api/goods/getGoodsList',
      method: 'post',
      data: {
        cate_id: currType,
        page: curp,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res);
        app.curpage = curp;
        var numpage = curp * 12;
        var goodslistjf2 = res.data.data;
        var goodslistjf3 = that.data.goodslistjf1.concat(goodslistjf2);
        app.goodslistjf3 = goodslistjf3;
        that.setData({
          goodslistjf1: goodslistjf3,
          goodslistjf3: goodslistjf3,
          numpage: numpage,
        });      
        if (goodslistjf2.length == 0 ){
          that.setData({
            ism:1,
            morewz: '没有更多了。。。',
          });         
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });


  },


  // 拨打电话
  opentel: function () {
    app.gotel();
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },

})
app.currType = 5;
app.curpage = 1;
app.goodslistjf3 = [];